package com.sff.manager {
	
	import com.sff.core.sff_internal;
	import com.sff.site.tree.Tree;
	import com.sff.site.tree.TreeDescriptor;
	import com.sff.site.tree.TreesProvider;
	
	import flash.net.URLRequest;

	public class DataManager {
		
		sff_internal var _tree 	: Tree;
		
		protected var _xmlStatic: XML;
		protected var _xmlData 	: XML;
		protected var _xml 		: XML;
		protected var _moduleUrl: String;
		protected var _parent 	: DataManager;
		protected var _staticUrl: String;
		protected var _styles	: Array = [];		
		protected var _fonts	: Array = [];				
		
		public function DataManager( datas : XML, parent : DataManager) {
			_xml = datas;
			_parent = parent;
			
			_parse( _xml , parent );
		}
		
		protected function _parse(xml : XML, parent : DataManager ) : void {
			
			//moduleUrl
			if( xml.moduleUrl.length() > 0 && xml.moduleUrl.toString() != "" ) {
				_moduleUrl = xml.moduleUrl.toString();				
			}
			
			//siteMap
			sff_internal::_tree = TreesProvider.sff_internal::parseSiteMap(xml.siteMap[0]);
				
			//data
			if (_xml.data && _xml.data.toString() != '') {
				_xmlData = _xml.data[0];
			}
			
			//static
			if (_xml.static && _xml.static.toString() != '') {
				_staticUrl = _xml.static.toString();
			}
		}
		
		public function get parent() : DataManager {
			return _parent;
		}

		public function get moduleURL() : String {
			return _moduleUrl;
		}
				
		public function get xmlData() : XML {
			return _xmlData;
		}
		
		public function get staticURL() : String {
			return _staticUrl;
		}
		
		public function get styles() : Array {
			return _styles;
		}
		
		public function get fonts() : Array {
			return _fonts;
		}
		
		public function get xmlStatic() : XML {
			return _xmlStatic;
		}
		
		public function dispose() : void {
			sff_internal::_tree.dispose( );			
		}
		
		sff_internal function get tree() : Tree {
			return sff_internal::_tree;
		}
		
		sff_internal function parseStatic(xml:XML) : void {
			
			if (xml == null || !xml) {
				return;
			}
			
			//styles
			if (xml.styles && xml.styles.toString() != '') {
				for each (var style: XML in xml.styles.style) {
					_styles.push(style.toString());
				}
			}
			
			//fonts
			if (xml.fonts && xml.fonts.toString() != '') {
				for each (var font: XML in xml.fonts.font) {
					_fonts.push(font.toString());
				}
			}
			
			//static value
			if (xml.static && xml.static.toString() != '') {
				_xmlStatic = xml.static[0];
			}
		}		
	}
}
